#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <sys/wait.h>
#include <pthread.h>

void func(int num)
{
	pid_t pid;
	int status;
	while((pid = waitpid(-1, &status, 0)) != -1)
	{
		if(WIFEXITED(status))
		printf("waitpid success : %d  return: %d\n", pid, WEXITSTATUS(status));
	}
	return;
}

int main()
{
	int i;
	pid_t pid;
	sigset_t set;
	sigemptyset(&set);
	sigaddset(&set, SIGCHLD);
	sigprocmask(SIG_BLOCK,&set,NULL);

	for(i = 0; i < 15; i++)
		if((pid = fork()) == 0)
		break;

	if(15 == i)
	{
		struct sigaction act,oldact;
		act.sa_handler = func;
		act.sa_flags = 0;
		sigemptyset(&act.sa_mask);
		sigaction(SIGCHLD, &act, NULL);
		sigprocmask(SIG_UNBLOCK,&set,NULL);

		printf("parent : %d\n", getpid());
		while(1);
	}
	else
	{
		printf("child : %d\n", getpid());
		return i;
	}
	
	return 0;
}
